﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace HTF_HMI
{
    public partial class AUDIT_PERIOD_EDIT : Form
    {      
        public bool isAdd = true;
        public string reportId = string.Empty;        
        private AUDIT_PERIOD audit_period;

        public AUDIT_PERIOD_EDIT(AUDIT_PERIOD act_form,string id)
        {
            InitializeComponent();
            audit_period = act_form;
            BindArea();         
            if (!string.IsNullOrEmpty(id))
            {
                reportId = id;
                isAdd = false;
                BindPeriod(reportId);                      
            }           
        }

        private void BindArea()
        {
            var area = DBClass.DBHelper.GetAuditArea();
            cmb_area.DisplayMember = "AREA_NAME";
            cmb_area.ValueMember = "AREA_ID";
            cmb_area.DataSource = area;           
        }

        private void BindPeriod(string id)
        {
            var period = DBClass.DBHelper.GetAuditPeriod(id).FirstOrDefault();
            if (period != null)
            {
                txt_id.Text = id;
                cmb_area.Text = period.AREA_NAME;               
                cmb_task.Text = period.TASK_NAME;
                cmb_shift.Text = period.SHIFT;
                date_base.Text = period.BASE_DATE.ToString();
                date_start.Time = DateTime.Parse(period.START_TIME);
                date_end.Time = DateTime.Parse(period.END_TIME);
                cmb_type.Text = period.TYPE;
                txt_period.Text = period.PERIOD_TIME.ToString();
                cmb_class.Text = period.CLASS_SET;
                cb_holiday.Checked = (period.HOLIDAY == "是");
                cb_valid.Checked = (period.ENABLE == "是");
                txt_remark.Text = period.REMARK;
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string strArea = cmb_area.Text;
            string strClass = cmb_class.Text;
            string strShift = cmb_shift.Text;
            string strTaskId = cmb_task.SelectedValue.ToString();
            string strTaskName = cmb_task.Text;
            string strType = cmb_type.Text;
            string strPeriod = txt_period.Text;
            string strRemark = txt_remark.Text;
            string strBaseDate = date_base.Text;
            string srtStartTime = date_start.Text;
            string srtEndTime = date_end.Text;

            if (string.IsNullOrEmpty(strArea))
            {
                MessageBox.Show("请选择巡检区域！");
                return;
            }
            if (string.IsNullOrEmpty(strTaskId))
            {
                MessageBox.Show("请选择巡检任务！");
                return;
            }
            if (string.IsNullOrEmpty(strShift))
            {
                MessageBox.Show("请选择班次！");
                return;
            }
            if (string.IsNullOrEmpty(strBaseDate))
            {
                MessageBox.Show("请选择基准开始日期！");
                return;
            }
            if (string.IsNullOrEmpty(srtStartTime))
            {
                MessageBox.Show("请选择计划开始日期！");
                return;
            }
            if (string.IsNullOrEmpty(srtEndTime))
            {
                MessageBox.Show("请选择计划结束日期！");
                return;
            }
            if (string.IsNullOrEmpty(strType))
            {
                MessageBox.Show("请选择巡检类型！");
                return;
            }

            if (strType.Contains("自定义"))
            {
                if (string.IsNullOrEmpty(strPeriod))
                {
                    MessageBox.Show("请输入频次！");
                    return;
                }
                else
                {
                    if (!IsNumeric(strPeriod))
                    {
                        MessageBox.Show("频次必须为正整数！");
                        return;
                    }
                }
            }
            if (string.IsNullOrEmpty(strClass))
            {
                MessageBox.Show("请选择班制！");
                return;
            }

            try
            {
                string sql = string.Empty;
                DBEntity.Models.AUDIT_PERIOD_SET period = new DBEntity.Models.AUDIT_PERIOD_SET();
                period.AREA_NAME = strArea;
                period.BASE_DATE = DateTime.Parse(strBaseDate);
                period.CLASS_SET = strClass;
                period.ENABLE = cb_valid.Checked ? "是" : "否";
                period.HOLIDAY = cb_holiday.Checked ? "是" : "否";
                period.END_TIME = srtEndTime;
                period.START_TIME = srtStartTime;
                period.TASK_ID = Int32.Parse(strTaskId);
                period.TASK_NAME = strTaskName;
                period.TYPE = strType;
                period.PERIOD_TIME = string.IsNullOrEmpty(strPeriod)? 0:Int32.Parse(strPeriod);
                period.SHIFT = strShift;
                period.REMARK = strRemark;
                if (!isAdd)
                {
                    period.ID = Int32.Parse( txt_id.Text);
                    bool result = DBClass.DBHelper.UpdateAuditPeriod(period);
                    if (result)
                    {
                        MessageBox.Show("保存成功！");
                        this.DialogResult = DialogResult.OK;

                        audit_period.btn_query_Click(null, null);
                    }
                }
                else
                {
                    DBEntity.Models.AUDIT_PERIOD_SET newDevice = DBClass.DBHelper.InsertAuditPeriod(period);
                    if (newDevice != null)
                    {
                        MessageBox.Show("保存成功！");
                        this.DialogResult = DialogResult.OK;

                        audit_period.btn_query_Click(null, null);
                    }
                    else
                    {
                        MessageBox.Show("保存失败！");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                DBClass.Log.InsertLog("保存失败！" + ex.Message);
            }
        }

        private void cmb_area_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmb_task.ResetText();
            var task = DBClass.DBHelper.GetAuditTask(cmb_area.Text);
            cmb_task.DisplayMember = "TASK_NAME";
            cmb_task.ValueMember = "TASK_ID";
            cmb_task.DataSource = task;          
        }

        static bool IsNumeric(string str)
        {
            System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[0-9]\d*$");
            return reg1.IsMatch(str);
        }
    }
}
